package utils

import (
	"fmt"
	"github.com/spf13/viper"
)

// json file config
var (
	Host string
	Port int64
)

// yaml file config
var (
	BaseApiURL      string
	ServerURLMap    = make(map[string]string, 0)
	ServerAESKeyMap = make(map[string]string, 0)
)

// json file
func InitServerRun() error {
	viperJson := viper.New()
	viperJson.SetConfigName("config/server_run.json")
	viperJson.SetConfigType("json")
	viperJson.AddConfigPath(".")

	err := viperJson.ReadInConfig()
	if err != nil {
		return err
	}
	// fmt.Println(">>> ", viperJson)
	Host = viperJson.Get("host").(string)
	PortFloat64 := viperJson.Get("port").(float64) // viper默认将数字转为了float64类型
	Port = int64(PortFloat64)
	fmt.Println("host: ", Host, "port: ", Port)
	return nil
}

// yaml file
func InitSecretConfig() error {
	viperYaml := viper.New()
	viperYaml.SetConfigName("config/secret_config.yaml")
	viperYaml.SetConfigType("yaml")
	viperYaml.AddConfigPath(".")

	err := viperYaml.ReadInConfig()
	if err != nil {
		return err
	}
	//fmt.Println("yaml: ", viperYaml)

	BaseApiURL = viperYaml.Get("BaseApiURL").(string)
	ServerURLMapInter := viperYaml.Get("ServerURL").(map[string]interface{})
	ServerAESKeyMapInter := viperYaml.Get("ServerAESKey").(map[string]interface{})
	// 构建ServerURLMap
	for key, val := range ServerURLMapInter {
		strVal := fmt.Sprintf("%v", val)
		ServerURLMap[key] = strVal
	}
	// 构建ServerAESKeyMap
	for key, val := range ServerAESKeyMapInter {
		strVal := fmt.Sprintf("%v", val)
		ServerAESKeyMap[key] = strVal
	}

	fmt.Println("BaseApiURL: ", BaseApiURL)
	fmt.Println("ServerURLMap: ", ServerURLMap)
	fmt.Println("ServerAESKeyMap: ", ServerAESKeyMap)

	return nil
}
